#include "filescanner.h"
#include <QDir>




FileScanner::FileScanner(QObject *parent) :
    QObject(parent)
{
}

FileScanner::FileScanner(QString dir, QObject *parent) :
    QObject(parent)
{
    scanFolder(dir);
}


QList<QList<QFileInfo> > FileScanner::scanFolder(QString dir)
{
    scanDirRecursively(dir);

    QList<QList<QFileInfo> > list;
    for (int i = 0 ; i < mapFolderFiles.values().size() ; i++)
    {
        list.append(mapFolderFiles.values().at(i));
    }

    return list;
}


void FileScanner::scanDirRecursively(QString dir)
{
    QDir folder(dir);
    bool test = folder.exists();
    QFileInfoList list = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);

    for (int i = 0 ; i < list.size() ; i++)
    {
        QFileInfo fileInfo = list.at(i);

        if (fileInfo.isDir())
        {
            scanDirRecursively(fileInfo.absolutePath());
        }
        else
        {
            QList<QFileInfo> listFiles = mapFolderFiles.value(fileInfo.absoluteDir().absolutePath());
            listFiles.append(fileInfo);
            mapFolderFiles.insert(fileInfo.absoluteDir().absolutePath(),listFiles);
        }
    }
}
